__author__ = 'wenke1'

from xmlrpclib import ServerProxy
from cmd import Cmd
from fileshare import FileNode
from threading import Thread
import time
import sys

class Client(Cmd):
    prompt = '>'
    def __init__(self,url,sharedir,passwd):
        Cmd.__init__(self)
        self.url=url
        self.sharedir=sharedir
        self.passwd=passwd
    def help_start(self):
        print "start command:start server and client"
    def do_start(self,arg):
        #start share service
        self.server=FileNode(self.url,self.sharedir,self.passwd)
        t=Thread(target=self.server._start)
        t.setDaemon(1)
        t.start()
        time.sleep(5)
        self.client=ServerProxy(self.url)
    def do_getFile(self,queryfile):
        try:
            self.client.getFile(queryfile,self.passwd)
        except:
            print("get file fail!")
    def do_exit(self,arg):
        sys.exit()

if __name__=='__main__':
    url,sharedir,passwd=sys.argv[1:]
    shareFileClient=Client(url,sharedir,passwd)
    shareFileClient.cmdloop()




